﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ProgramMain.Database;
using ProgramMain.Layers;

namespace ProgramMain.NetVis
{
   
    public class CableViz : CrossBoxVis
    {
        [XmlIgnore]
        new public SimpleOpticNetDB.CableRow Row { get; private set; }

        public void SetRow(SimpleOpticNetDB.CableRow row)
        {
            Row = row;
            if (Row != null)
            {
                ID = Row.ID;
                Caption = row.IsCaptionNull() ? "" : row.Caption;
            }
        }

        public override void CalcVirtualPoint()
        {
              EquipmentSerializer e = Vis.GetMapProperty(ID);
              if (e != null && e.VirtualPoint != Point.Empty)
              {
                  VirtualPoint = e.VirtualPoint;
                  Enter = e.Enter;
              }
              else
              {
                  Size size = CalcSize();
                  int q = 0;
                  int curveCount = Vis.CurveElementCount();
                  Math.DivRem(IndexMap, 4, out q);
                  CrossBoxVis r = Vis.FindLocation(IndexMap, CurveElementKindID);
                  switch (q)
                  {
                      case 0:
                          VirtualPoint =
                              new Point((-1)*(size.Width + body.shift + (r != null ? Math.Abs(r.VirtualPoint.X) : 0)),
                                        (-1)*(size.Height + body.cableShift));
                          Enter = 1;
                          break;
                      case 1:
                          VirtualPoint =
                              new Point(body.shift + (r != null ? Math.Abs(r.VirtualPoint.X) + r.CalcSize().Width : 0),
                                        curveCount > 1 ? body.cableShift : body.shift);
                          Enter = 2;
                          break;
                      case 2:
                          VirtualPoint =
                              new Point(body.shift + (r != null ? Math.Abs(r.VirtualPoint.X) + r.CalcSize().Width : 0),
                                        (-1)*(size.Height + body.cableShift));
                          Enter = 1;
                          break;
                      case 3:
                          VirtualPoint =
                              new Point((-1)*(size.Width + body.shift + (r != null ? Math.Abs(r.VirtualPoint.X) : 0)),
                                        curveCount > 1 ? body.cableShift : body.shift);
                          Enter = 2;
                          break;
                  }
              }
            if (body != null) body.VirtualPoint = CalcBodyPoint();
        }
    }
}